package cn.tedu.jdbc;

import java.sql.*;
import java.util.Scanner;

//作业练习
public class HomeWork {
    public static void main(String[] args) throws Exception {
        method1();
        method2();
        method3();
    }

    private static void method1() throws Exception {
        System.out.println("请输入用户名");
        String a = new Scanner(System.in).nextLine();
        System.out.println("请输入密码");
        String b = new Scanner(System.in).nextLine();
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql:///abc?characterEncoding=utf8";
        Connection conn = DriverManager.getConnection(url, "root", "root105");
        String sql = "SELECT * FROM user_1 WHERE name =? and password =?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, a);
        ps.setString(2, b);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            System.out.println("登录成功");
        } else {
            System.out.println("登录失败");
        }
        rs.close();//结果集
        ps.close();//传输器
        conn.close();//连接
    }

    private static void method2() throws Exception {
        System.out.println("请输入用户名");
        String a = new Scanner(System.in).nextLine();
        System.out.println("请输入密码");
        String b = new Scanner(System.in).nextLine();
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql:///abc?characterEncoding=utf8";
        Connection conn = DriverManager.getConnection(url, "root", "root105");
        String sql = "SELECT * FROM user_1 WHERE name =? and password =?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, a);
        ps.setString(2, b);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            System.out.println("登录成功");
        } else {
            System.out.println("登录失败");
        }
        rs.close();//结果集
        ps.close();//传输器
        conn.close();//连接
    }

    private static void method3() throws Exception {
        System.out.println("请输入用户名");
        String a = new Scanner(System.in).nextLine();
        System.out.println("请输入密码");
        String b = new Scanner(System.in).nextLine();
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql:///abc?characterEncoding=utf8";
        Connection conn = DriverManager.getConnection(url, "root", "root105");
        String sql = "SELECT * FROM user_1 WHERE name =? and password =?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, a);
        ps.setString(2, b);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            System.out.println("登录成功");
        } else {
            System.out.println("登录失败");
        }
        rs.close();//结果集
        ps.close();//传输器
        conn.close();//连接
    }
}
